package com.dongdongshop.operator.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.front.pojo.TbContent;
import com.dongdongshop.front.pojo.TbContentCategory;
import com.dongdongshop.front.service.ContentCategoryService;
import com.dongdongshop.front.service.ContentService;
import com.dongdongshop_util.OSSutils;
import com.dongdongshop_util.Result;
import com.dongdongshop_util.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    private final Logger logger = LoggerFactory.getLogger(ContentCategoryController.class);

    @Reference(check = false)
    private ContentService contentService;
    //调用广告类型
    @Reference(check = false)
    private ContentCategoryService contentCategoryService;

    //跳转到广告管理
    @RequestMapping("toContent")
    public String toContent(Model model){
        List<TbContentCategory> contentCategory = contentCategoryService.getContentCategoryOrName(null);
        model.addAttribute("contentCategory",contentCategory);
        return "admin/content";
    }

    //获取广告表
    @ResponseBody
    @RequestMapping("getContent")
    public Result getContent(){
        try {
            List<TbContent> contentList = contentService.getContent();
            return Result.success(ResultEnum.SUCCESS,contentList);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //增加---修改
    @ResponseBody
    @RequestMapping("addOrUpdate")
    public Result addOrUpdate(TbContent content){
        try {
            if (content.getId() == null){
                contentService.addContent(content);
            }else {
                contentService.updateContent(content);
            }
            return Result.perfect(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //oss上传图片
    @ResponseBody
    @RequestMapping("upload")
    public Result upload(@RequestParam("myFile") MultipartFile myFile){
        try {
            String upload = OSSutils.upload(myFile);
            return Result.success(ResultEnum.SUCCESS,upload);
        } catch (Exception e){
            logger.error(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //回显操作
    @ResponseBody
    @RequestMapping("getContentById")
    public Result getContentById(Long id){
        try {
           TbContent content = contentService.getContentById(id);
            return Result.success(ResultEnum.SUCCESS,content);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
    //批量删除
    @ResponseBody
    @RequestMapping("deleteContent")
    public Result deleteContent(@RequestParam(value = "ids[]")Long[] ids){
        try {
            contentService.deleteContent(ids);
            return Result.perfect(ResultEnum.SUCCESS);
        }catch (Exception e){
            logger.info(String.valueOf(ResultEnum.FAIT),e);
            return Result.response(ResultEnum.FAIT);
        }
    }
}
